# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>

_realname=plplot

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.10.0
pkgrel=1
arch=('any')
pkgdesc="Scientific plotting software (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
    "${MINGW_PACKAGE_PREFIX}-gcc-ada"
    "${MINGW_PACKAGE_PREFIX}-freetype"
    "${MINGW_PACKAGE_PREFIX}-tk" 
    "${MINGW_PACKAGE_PREFIX}-wxWidgets"
    "${MINGW_PACKAGE_PREFIX}-lua"
    "${MINGW_PACKAGE_PREFIX}-python2"
    "${MINGW_PACKAGE_PREFIX}-python2-numpy"
    "${MINGW_PACKAGE_PREFIX}-qhull")
makedepends=("subversion"
    "${MINGW_PACKAGE_PREFIX}-cmake" 
    "${MINGW_PACKAGE_PREFIX}-gcc-fortran" 
    "${MINGW_PACKAGE_PREFIX}-gcc-ada")
options=('strip' 'staticlibs')
license=('LGPL')
url="http://svn.code.sf.net/p/plplot"
source=(http://downloads.sourceforge.net/sourceforge/plplot/${_realname}-${pkgver}.tar.gz
    find-gd.patch)
sha1sums=('ea962cb0138c9b4cbf97ecab1fac1919ea0f939f'
          '7800cad653ebcedf596a01e545116f6ffd04f0f9')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/find-gd.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
    export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  pushd ${MINGW_PREFIX} > /dev/null
    export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  # cmake's FindSWIG doesn't work properly on MSYS,
  # so we provide SWIG here
  SWIG_EXECUTABLE_U=`type -p swig`
  SWIG_EXECUTABLE=`cygpath -m ${SWIG_EXECUTABLE_U}`.exe
  SWIG_DIR_U=`${SWIG_EXECUTABLE_U} -swiglib`
  SWIG_DIR=`cygpath -m $SWIG_DIR_U`

  # cmake may be confused if there is a Windows installation
  # of the official Python MSI. Please do the Python-enabled
  # build only, if no official Python is installed.
  cd "$srcdir"
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DCMAKE_Fortran_COMPILER:PATH=${MINGW_PREFIX}/bin/gfortran.exe \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DSWIG_EXECUTABLE="${SWIG_EXECUTABLE}" \
    -DSWIG_DIR="${SWIG_DIR}" \
    -DQHULL_INCLUDE_DIR=${PREFIX_DEPS}/include \
    -DGD_INCLUDE_DIR=${PREFIX_DEPS}/include \
    -DENABLE_lua=ON \
    -DENABLE_python=ON \
    ../${_realname}-${pkgver}
  make V=1
}

package() {
  cd "$srcdir/build-${MINGW_CHOST}"
  make install # DESTDIR="$pkgdir"
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname  
  cp -pf ${srcdir}/${_realname}-${pkgver}/Copyright \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname
  cp -pf ${srcdir}/${_realname}-${pkgver}/COPYING* \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname
}
